
<template>
    <div style="border: 1px solid #ccc;">
        <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" />
        <Editor style="height: 301px; overflow-y: hidden;" v-model="html" :defaultConfig="editorConfig" :mode="mode"
            @onCreated="onCreated" @onChange="onChange" />
    </div>
</template>
<script>
import Vue from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
export default Vue.extend({
    components: { Editor, Toolbar },
    props: [],
    data() {
        return {
            editor: null,
            html: '',
            toolbarConfig: {},
            editorConfig: {
                placeholder: '请输入内容...',
                MENU_CONF: {
                    // 所有的菜单配置，都要在 MENU_CONF 属性下
                    // 配置上传图片
                    uploadImage: {
                        server: 'http://134.175.252.64:8080/file/upload',
                        // form-data fieldName，后端接口参数名称，默认值wangeditor-uploaded-image
                        fieldName: "file",
                    },
                },
                autoFocus: false

            },
            mode: 'default', // or 'simple'
        }
    },
    mounted() {

        // 模拟 ajax 请求，异步渲染编辑器
        // setTimeout(() => {
        //     this.html = ''
        // }, 1500)
        this.$bus.$on('getForm', this.getForm)
        this.$bus.$on('content', this.initHtml)
        // 
        // this.$emit('getContent', editor.getHtml())
    },
    methods: {
        onCreated(editor) {
            this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        },
        onChange(editor) {
            this.$emit('getContent', this.html)
        },
        getForm(rowObj) {
            const editor = this.editor // 获取 editor 实例
            if (editor == null) return
            // if (rowObj.content.length > 0) {
            const content = rowObj.content
            editor.setHtml(content)
            // } else {
            // editor.setHtml('')
            // }
        },
        initHtml(content) {
            const editor = this.editor // 获取 editor 实例
            if (editor == null) return
            editor.setHtml(content)
        }
    },
    beforeDestroy() {
        const editor = this.editor
        if (editor == null) return
        editor.destroy() // 组件销毁时，及时销毁编辑器
        this.$bus.$off(['getForm', 'content'])
    }
});
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<!-- <style lang='' scoped>
</style> -->